import { Ast, Visitor } from "./ast";
export interface LiveRoomInfo {
    name?: string;
    online?: number;
    rank?: string;
    category?: string;
    redPacketCount?: number;
    status?: string;
    openTime?: string;
    close?: () => void;
    follow?: () => void;
    more?: () => void;
    comment?: () => void;
    gift?: () => void;
    share?: () => void;
    open?: () => void;
}
export function getLiveRoomInfo(node: Ast) {
    const ctx: LiveRoomInfo = {}
    const visitor = new LiveRoomInfoVisitor();
    node.visit(visitor, ctx)
    if (ctx.name) {
        // ctx.status = `已结束`
        if(ctx.status === '已结束'){
            return;
        }
        return ctx;
    }
}
export class LiveRoomInfoVisitor implements Visitor<LiveRoomInfo> {
    visitNode(node: Ast, ctx: LiveRoomInfo) {
        if (!node.visibleToUser) return;
        if (node.className === `android.widget.TextView`) {
            return this.visitTextView(node, ctx)
        }
        if (node.className == `android.widget.FrameLayout`) {
            return this.visitFrameLayout(node, ctx)
        }
        if (node.className === `androidx.viewpager.widget.ViewPager`) {
            return this.visitViewPager(node, ctx)
        }
        if (node.className === `android.widget.RelativeLayout`) {
            return this.visitRelativeLayout(node, ctx)
        }
        if (node.className === `android.widget.LinearLayout`) {
            return this.visitLinearLayout(node, ctx)
        }
        if (node.className === `android.widget.TextSwitcher`) {
            return this.visitTextSwitcher(node, ctx)
        }
        node.children.map(child => child.visit(this, ctx))
    }
    visitRelativeLayout(node: Ast, ctx: LiveRoomInfo) {
        if (node.id.endsWith(`live_bottom_bar_single_icon_container`) && node.clickable) {
            ctx.share = () => node.click()
            return;
        }
        node.children.map(child => child.visit(this, ctx))
    }
    visitTextSwitcher(node: Ast, ctx: LiveRoomInfo) {
        if (node.id.endsWith(`live_comment_text_view`) && node.clickable) {
            ctx.comment = () => node.click()
            return;
        }
        node.children.map(child => child.visit(this, ctx))
    }
    visitViewPager(node: Ast, ctx: LiveRoomInfo) {
        if (node.id.endsWith(`live_slide_view_pager`)) {
            return node.children.map(child => child.visit(this, ctx))
        }
        node.children.map(child => child.visit(this, ctx))
    }
    visitFrameLayout(node: Ast, ctx: LiveRoomInfo) {
        if (!node.visibleToUser) return;
        if (node.id.endsWith(`live_play_root_container`) && node.visibleToUser) {
            return node.children.map(child => child.visit(this, ctx))
        }
        if (node.id.endsWith(`live_square_side_bar_layout_close_view`) && node.clickable) {
            ctx.close = () => node.click()
            return;
        }
        if (node.id.endsWith(`live_bottom_bar_gift_container`) && node.clickable) {
            ctx.gift = () => node.click()
            return;
        }
        node.children.map(child => child.visit(this, ctx))
    }
    visitLinearLayout(node: Ast, ctx: LiveRoomInfo) {
        if (node.id.endsWith(`live_right_pendant_container`) && node.clickable) {
            ctx.more = () => node.click()
            return;
        }
        node.children.map(child => child.visit(this, ctx))
    }
    visitTextView(node: Ast, ctx: LiveRoomInfo) {
        if (node.id.endsWith(`live_play_closed_v3_title_text_view`)) {
            if (node.text === `直播已结束`) {
                ctx.status = `已结束`;
            }
            return;
        }
        if (node.id.endsWith(`status_text_view`) && node.text === `很遗憾，本次未中奖`) {
            ctx.status = `已结束`
            return;
        }
        if (node.id.endsWith(`coupon_top_title`) && node.text === `手慢了，补偿一张充值券`) {
            ctx.status = `已结束`
            return;
        }
        if (node.id.endsWith(`empty_desc_text_view`) && node.text === `很遗憾，本次任务失败`) {
            ctx.status = `已结束`
            return;
        }
        if (node.id.endsWith(`live_name_text`)) {
            ctx.name = node.text
            return;
        }
        if (node.id.endsWith(`live_follow_text`)) {
            ctx.follow = () => node.click()
            return;
        }
        if (node.id.endsWith(`live_audience_count_text`)) {
            ctx.online = parseInt(node.text)
            return;
        }
        if (node.id.endsWith(`live_hourly_rank_content_view`)) {
            ctx.rank = node.text
            return;
        }
        if (node.id.endsWith(`live_side_bar_pendant_text_view`)) {
            ctx.category = node.text
            return;
        }
        if (node.id && node.id.endsWith('live_red_packet_count')) {
            ctx.redPacketCount = parseInt(node.text)
            return;
        }
        if (node.id.endsWith(`live_red_packet_status_text`) && node.visibleToUser) {
            ctx.status = node.text;
            ctx.openTime = node.formatDate(node.second(node.toSecond()))
            if (node.parent.clickable) {
                ctx.open = () => {
                    node.parent.click()
                }
            } else {
                toastLog(`红包按钮不可点击`)
                ctx.open = () => {
                    node.parent.clickRect()
                }
            }
        }
    }
}
